/*********** NOTES HEADER *********** 


************************************/ 
cd "N:\MedicareClaims-P045601-BE\Work\hosp_retro\health_out\Program\PropensityScore\Patients\ResponsetoReferee\LeadsLags"
set more off
capture log close
clear all

global origData "N:\MedicareClaims-P045601-BE"
global logs "N:\MedicareClaims-P045601-BE\Work\hosp_retro\health_out\Logs\PropScore"
global dataIn "N:\MedicareClaims-P045601-BE\Work\hosp_retro\health_out\Data-In\"
global dataOut "N:\MedicareClaims-P045601-BE\Work\hosp_retro\health_out\Data-Out\PropScore"
global Output "N:\MedicareClaims-P045601-BE\Work\hosp_retro\health_out\Output\Figures\Results\PropScore"
global dpath "N:\MedicareClaims-P045601-BE\Work\ay_data"
global skapath "N:\MedicareClaims-P045601-BE\Work\ska"

adopath +  N:\SIL-Common\estout
adopath +  N:\SIL-Common\outreg2

adopath +  N:\SIL-Common\estout
adopath +  N:\SIL-Common\outreg2


forvalues j = 1(1)2 {
use ".\paralleltrends_summary_sm.dta"
keep if sex_pat ==`j'
drop sex_pat
capture drop _merge

tostring vmerger, replace
gen treat="_"+vmerger
drop vmerger
reshape wide oc* , i(leadlag) j(treat) str

foreach var of varlist oc* {
if regexm("`var'","0") {
 local vk = regexr("`var'","oc_","mef_beta_")
 local vk = regexr("`vk'","_0","")
 di "`vk'"
 rename `var' `vk'
  }
  else {
 local vk = regexr("`var'","oc_","mem_beta_")
 local vk = regexr("`vk'","_1","")
 di "`vk'"
 rename `var' `vk'
  }
  
 }

gen id=_n-1
replace id=id+1 if _n>16
replace id=16 if _n==34
drop if id>32
sort id
order id


foreach i of varlist me* {
 replace `i' = 0  if `i' ==.
} 

reshape long mem_beta_ mef_beta_ , i(id) j(depvar) str
replace leadlag = leadlag - 16

rename mem_beta_ beta_m
rename mef_beta_ beta_f

replace depvar = trim(depvar)
replace depvar=regexr(depvar,"_","")

gen dvar="Acute heart (%)" if depvar=="acuteicd8"
replace dvar="AMI (%)" if depvar=="ami"                                         
replace dvar="Congestive heart failure (%)" if depvar=="chf"
replace dvar="Mortality (%)" if depvar=="death"                                         
replace dvar="Diabetes complications (%)" if depvar=="diabcomp" 
replace dvar="Asymptomatic (%)" if depvar=="diabcomp1"                                         
replace dvar="Symptomatic (%)" if depvar=="diabcomp2"                                         
replace dvar="Glaucoma (%)" if depvar=="glaucoma"
replace dvar="Ischemic heart (%)" if depvar=="ihd"
replace dvar="Malignant Hypertension (%)" if depvar=="malighyp"                                         

drop if leadlag < -12
drop if leadlag >  12

graph set window fontface "Times New Roman"
graph set ps fontface "Times New Roman"

label var beta_m "Acquired"
label var beta_f "Non-acquired"

gen pct_f=beta_f*100
gen pct_m=beta_m*100

label var pct_m "Acquired (%)"
label var pct_f "Non-acquired (%)"

foreach i in "glaucoma" "diabcomp1" "diabcomp2" "ihd" "ami" "acuteicd8"  {
levelsof dvar if depvar=="`i'", local(k)

twoway line pct_f pct_m leadlag if depvar=="`i'" , ///
xtitle("Quarters since merger", size(small) margin(t .5))  ///  
ytitle(`k', margin(r 1 l -1) axis(1) size(small)) ///
lp(solid dash) saving(g`i', replace)  graphregion(color("white")) scheme(s2mono) ///
xline(0)  
}

foreach i in "death" {
levelsof dvar if depvar=="`i'", local(k)


if `j'==1 {
twoway line pct_f pct_m leadlag if depvar=="`i'" , ///
title("Male") ///
xtitle("Quarters since merger", size(small) margin(t .5))  ///  
ytitle(`k', margin(r 1 l -1) axis(1) size(small)) ///
lp(solid dash) saving(g`i'`j', replace)  graphregion(color("white")) scheme(s2mono) ///
xline(0)  
}


if `j'==2 {
twoway line pct_f pct_m leadlag if depvar=="`i'" , ///
title("Female") ///
xtitle("Quarters since merger", size(small) margin(t .5))  ///  
ytitle(`k', margin(r 1 l -1) axis(1) size(small)) ///
lp(solid dash) saving(g`i'`j', replace)  graphregion(color("white")) scheme(s2mono) ///
xline(0)  
}

}

if `j'==1 {
graph combine gglaucoma.gph gdiabcomp1.gph gdiabcomp2.gph , col(1) xcommon 
graph export "ParallelTrends_Diabetes`j'.ps", as(ps) orientation(landscape) lmargin(.05) tmargin(.3) mag(200) logo(off) replace

graph combine gami.gph gihd.gph gacuteicd8.gph , col(1) xcommon 
graph export "ParallelTrends_Hypertension`j'.ps", as(ps) orientation(landscape) lmargin(.05) tmargin(.3) mag(200) logo(off) replace


}


if `j'==2 {
graph combine gglaucoma.gph gdiabcomp1.gph gdiabcomp2.gph , col(1) xcommon 
graph export "ParallelTrends_Diabetes`j'.ps", as(ps) orientation(landscape) lmargin(.05) tmargin(.3) mag(200) logo(off) replace

graph combine gami.gph gihd.gph gacuteicd8.gph , col(1) xcommon  
graph export "ParallelTrends_Hypertension`j'.ps", as(ps) orientation(landscape) lmargin(.05) tmargin(.3) mag(200) logo(off) replace

}


clear

}

graph combine gdeath1.gph gdeath2.gph, col(1) xcommon ycommon  
graph export "ParallelTrends_Mortality.ps", as(ps) orientation(landscape) lmargin(.05) tmargin(.3) mag(200) logo(off) replace
 
 
 
 